{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import f1_score,accuracy_score,recall_score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 特征为：将两句话连在一起，然后做tfidf-Vectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "def get_text(df):\n",
    "    df[\"text\"] = df[\"query1\"] + \" \" + df[\"query2\"]\n",
    "    return df[\"text\"].values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "train_data = pd.read_csv(\"../jupyter/shuffle-data/train_data.csv\")\n",
    "test_data = pd.read_csv(\"../jupyter/shuffle-data/test_data.csv\")\n",
    "train_text_corpus = get_text(train_data)\n",
    "test_text = get_text(test_data)\n",
    "\n",
    "vectorizer = TfidfVectorizer()\n",
    "train_X = vectorizer.fit_transform(train_text_corpus)\n",
    "train_y = train_data[\"label\"]\n",
    "\n",
    "test_X = vectorizer.transform(test_text)\n",
    "test_y = test_data[\"label\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构建LR分类器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf = LogisticRegression(random_state=2020).fit(train_X, train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "F1 Score: 0.048192771084337345\n",
      "acc Score: 0.6056572379367721\n",
      "recall Score: 0.024896265560165973\n"
     ]
    }
   ],
   "source": [
    "predict_y = clf.predict(test_X)\n",
    "\n",
    "print(\"F1 Score:\",f1_score(test_y,predict_y))\n",
    "print(\"acc Score:\",accuracy_score(test_y,predict_y))\n",
    "print(\"recall Score:\",recall_score(test_y,predict_y))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "tf2_py37",
   "language": "python",
   "display_name": "tf2_py37"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}